﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Service.Day
{
    /// <summary>
    /// 公司五日均值
    /// </summary>
    public class ServiceCompanyFiveDayAvg : Benefit.Interface.Day.ICompanyFiveDayAvg
    {
        Benefit.DB.DBManager db = null;
        public ServiceCompanyFiveDayAvg(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public void InitCompanyFiveDayAvg(int tradeHistoryId)
        {
            Models.Day.CompanyFiveDayAvg avg = new Models.Day.CompanyFiveDayAvg();
            var query = db.OperatorFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId);
            var queryExist = db.CompanyFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId);
            if (queryExist.Count() > 0)
            {
                queryExist.First().AvgBillBenefit = query.Average(a => a.AvgBillBenefit);
                queryExist.First().AvgPostionTime = query.Average(a => a.AvgPostionTime);
                queryExist.First().BillCount = query.Average(a => a.BillCount);
                queryExist.First().DayCount = query.Average(a => a.DayCount);
                queryExist.First().Free = query.Average(a => a.Free);
                queryExist.First().LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
                queryExist.First().MaxReturn = query.Average(a => a.MaxReturn);
                queryExist.First().OverStopLoss = query.Average(a => a.OverStopLoss);
                queryExist.First().PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
                queryExist.First().Profit = query.Average(a => a.Profit);
                queryExist.First().RaiseRate = query.Average(a => a.RaiseRate);
                queryExist.First().RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
                queryExist.First().SumLost = query.Average(a => a.SumLost);
                queryExist.First().SumWin = query.Average(a => a.SumWin);
                queryExist.First().UsedMargin = query.Average(a => a.UsedMargin);
                queryExist.First().Utilization = query.Average(a => a.Utilization);
                queryExist.First().WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
                queryExist.First().WinRate = query.Average(a => a.WinRate);
                queryExist.First().Yield = query.Average(a => a.Yield);
            }
            else
            {
                avg.AvgBillBenefit = query.Average(a => a.AvgBillBenefit);
                avg.AvgPostionTime = query.Average(a => a.AvgPostionTime);
                avg.BillCount = query.Average(a => a.BillCount);
                avg.DayCount = query.Average(a => a.DayCount);
                avg.Free = query.Average(a => a.Free);
                avg.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
                avg.MaxReturn = query.Average(a => a.MaxReturn);
                avg.OverStopLoss = query.Average(a => a.OverStopLoss);
                avg.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
                avg.Profit = query.Average(a => a.Profit);
                avg.RaiseRate = query.Average(a => a.RaiseRate);
                avg.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
                avg.SumLost = query.Average(a => a.SumLost);
                avg.SumWin = query.Average(a => a.SumWin);
                avg.UsedMargin = query.Average(a => a.UsedMargin);
                avg.Utilization = query.Average(a => a.Utilization);
                avg.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
                avg.WinRate = query.Average(a => a.WinRate);
                avg.Yield = query.Average(a => a.Yield);
                avg.TradeHistoryId = tradeHistoryId;

                db.CompanyFiveDayAvg.Add(avg);
            }

            db.SaveChanges();
        }

        public void Delete(int tradeHistoryId)
        {
            var query = db.CompanyFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            foreach (Models.Day.CompanyFiveDayAvg avg in query)
            {
                db.CompanyFiveDayAvg.Remove(avg);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public Models.Day.CompanyFiveDayAvg GetCompanyFiveDayAvg(int tradeHistoryId)
        {
            return db.CompanyFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).ToList().First();
        }
    }
}
